package org.ym.thread.application.results;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.ym.thread.application.model.ResizableCapacityLinkedBlockingQueue;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ym
 */
@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ThreadPoolInfo {
    /**
     * key
     */
    private Long key;
    /**
     * 当前任务数量 执行+队列
     */
    private Long taskCount;
    /**
     * 最大线程数
     */
    private Integer maximumPoolSize;
    /**
     * 核心线程数
     */
    private Integer corePoolSize;
    /**
     * 当前池中线程数量
     */
    private Integer poolSize;
    /**
     * 正在运行中的数量
     */
    private Integer activeCount;
    /**
     * 完成线程数量
     */
    private Long completedTaskCount;
    /**
     * 池历史最大数量
     */
    private Integer largestPoolSize;
    /**
     * 线程任务缓存队列当前使用长度
     */
    private Integer queueUseSize;

    /**
     * 线程任务缓存队列最大长度
     */
    private Integer queueSize;

    public static ThreadPoolInfo of(Long key, ThreadPoolExecutor threadPoolExecutor){
        if(key != null && threadPoolExecutor != null){
            ResizableCapacityLinkedBlockingQueue resizableCapacityLinkedBlockingQueue = (ResizableCapacityLinkedBlockingQueue) threadPoolExecutor.getQueue();
            return ThreadPoolInfo.builder()
                    .key(key)
                    .poolSize(threadPoolExecutor.getPoolSize())
                    .corePoolSize(threadPoolExecutor.getCorePoolSize())
                    .queueSize(resizableCapacityLinkedBlockingQueue.getCapacity())
                    .queueUseSize(resizableCapacityLinkedBlockingQueue.size())
                    .taskCount(threadPoolExecutor.getTaskCount())
                    .largestPoolSize(threadPoolExecutor.getLargestPoolSize())
                    .maximumPoolSize(threadPoolExecutor.getMaximumPoolSize())
                    .activeCount(threadPoolExecutor.getActiveCount())
                    .completedTaskCount(threadPoolExecutor.getCompletedTaskCount())
                    .build();
        }
        return null;
    }
}
